<?php

namespace app\ice\controller;

use org\BaiduMap;
use think\Db;
use think\Exception;
use think\Loader;
use think\Image;

class Auth extends Init
{

    protected function _initialize()
    {
        parent::_initialize();
        $this->DriverAuthModel = Loader::model('Auth');
        $this->DriverModel = Loader::model('Driver');
    }

    // 司机认证图片上传
    public function uploadAuthImage()
    {
        try {
            $param = input('post.');
            $type = [
                'driverHead', 'driversLicense', 'drivingLicense', 'scDrivingLicense', 'expressHeadImg', 'sfzzphoto', 'sfzfphoto'
            ];
            // 0,driverHead 司机头像 1,driversLicense 驾驶证 2,drivingLicense 行驶证 3,scDrivingLicense 手持行驶证
            if (empty($param['type']) || !in_array($param['type'], $type)) {
                throw new Exception('请传入类型');
            }
            $file = request()->file('image');
            $info = $file->validate(['size' => 111135678, 'ext' => 'jpg,png,gif,jpeg'])->rule('')->move(ROOT_PATH . 'public' . DS . "uploads/auth/{$param['type']}", md5('driver' . date('Ymd') . time() . rand(1, 10000)) . '.png');
            if ($info) {
//                $image = Image::open("uploads/auth/{$param['type']}/" . $info->getSaveName());
                returnApi(true, '上传成功', ['filename' => $info->getSaveName(), 'path' => "uploads/auth/{$param['type']}/" . $info->getSaveName()]);
            } else {
                returnApi(false, $file->getError());
            }
        } catch (Exception $e) {
            returnApi(false, $e->getMessage());
        }
    }

    // 人人帮认证
    public function realAuth()
    {
        Db::startTrans();
        try {
            $param = input('post.');
            if (!in_array($param['type'], [1, 2])) {
                throw new Exception('类型错误');
            }
            if (isset($param['lat']) && !empty($param['lat']) && !empty($param['lng']) && $param['type'] == 1) {
                $cid = BaiduMap::getCidByPoint($param['lng'], $param['lat']);
                Db::name('user')->where('id', $param['userId'])->update(['cid' => $cid, 'lat' => $param['lat'], 'lng' => $param['lng']]);
            }
            $res = Db::name('auth_real')->where([
                'type' => $param['type'],
                'realId' => $param['type'] == 2 ? $param['driverId'] : $param['userId']
            ])->find();
            if (!empty($res) && in_array($res['status'], [1, 2])) {
                throw new Exception('您已提交过审核');
            }
            $param['status'] = 1;
            if ($param['type'] == 2) {
                $param['realId'] = $param['driverId'];
            } else {
                $param['realId'] = $param['userId'];
            }
            if (empty($res)) {
                $res = model('AuthReal')->allowField(true)->save($param);
            } else {
                $res = model('AuthReal')->allowField(true)->save($param, [
                    'type' => $param['type'],
                    'realId' => $param['type'] == 2 ? $param['driverId'] : $param['userId']
                ]);
            }
            $res1 = true;
            if ($param['type'] == 1) {
                $res1 = Db::name('user')->where(['id' => $param['userId']])->update([
                    'rrbAuth' => $param['status']
                ]);
            } else if ($param['type'] == 2) {
                $res1 = Db::name('driver')->where(['driverId' => $param['driverId']])->update([
                    'rrbAuth' => $param['status']
                ]);
            }
            if ($res && $res1) {
                Db::commit();
                returnApi(true, '提交成功', [
                    'rrbAuth' => 1
                ]);
            } else {
                Db::rollback();
                returnApi(false, '提交失败', [
                    'rrbAuth' => 0
                ]);
            }
        } catch (Exception $e) {
            returnApi(false, $e->getMessage(), [
                'rrbAuth' => 0
            ]);
        }
    }

    // 获取认证信息
    public function getRealAuth()
    {
        try {
            $param = input('post.');
            $result = model('AuthReal')->where([
                'type' => $param['type'],
                'realId' => $param['type'] == 2 ? $param['driverId'] : $param['userId']
            ])->find();
            $result['headimg_path'] = config('IMG_HOST') . config('AUTH_PATH.expressHeadImg') . $result['headimg'];// 头像
            $result['faceimg_path'] = config('IMG_HOST') . config('AUTH_PATH.zfzzphoto') . $result['faceimg'];// 正面
            $result['conimg_path'] = config('IMG_HOST') . config('AUTH_PATH.zfzfphoto') . $result['conimg'];// 反面
            returnApi(true, '成功', $result);
        } catch (Exception $e) {
            returnApi(false, $e->getMessage());
        }
    }

    public function getMemberInfo()
    {
        try {
            $param = input('post.');
            if ($param['uid']) {
                $res = Db::name('user')->field('orderNum,m,rrbAuth,point')->where('id', $param['uid'])->find();
                $res['m'] = $res['m'] / 1000;
                returnApi(true, '成功', $res);
            }
        } catch (Exception $e) {
            returnApi(false, $e->getMessage());
        }
    }

    // 保存地址
    public function saveLocation()
    {
        try {
            $param = input('post.');
            if ($param['lat'] && $param['lng'] && $param['id']) {
                saveUserLocation($param['lng'], $param['lat'], $param['id'], '2');
                returnApi(true, '成功', []);
            }
        } catch (Exception $e) {
            returnApi(false, $e->getMessage());
        }
    }

}